/*
	GadgetFighterBay.cpp
	(c)2000 Palestar Inc, Richard Lyle
*/


#include "GadgetFighterBay.h"
#include "GameContext.h"
#include "Resource.h"

//----------------------------------------------------------------------------

Constant MAX_ZONE_FIGHTERS( "MAX_ZONE_FIGHTERS", 128 );

//----------------------------------------------------------------------------

IMPLEMENT_ABSTRACT_FACTORY( GadgetFighterBay, GadgetWeapon ); 
REGISTER_FACTORY_KEY( GadgetFighterBay, 4387377698697536802 );

BEGIN_ABSTRACT_PROPERTY_LIST( GadgetFighterBay, GadgetWeapon );
END_PROPERTY_LIST();

bool GadgetFighterBay::usable( Noun * pTarget, bool shift ) const
{
	return GadgetWeapon::usable( pTarget, shift );
}

const float		MAX_RECALL_RANGE = 10000.0f;

void GadgetFighterBay::use( dword when, Noun * pTarget, bool shift)
{
	if ( shift )
	{
		bool bFighterRecalled = false;

		// recall all fighters
		Array< GameContext::NounCollision > nouns;
		if ( context()->proximityCheck( worldPosition(), MAX_RECALL_RANGE, nouns, CLASS_KEY(NounFighter)) )
		{
			for(int i=0;i<nouns.size();i++)
			{
				NounFighter * pFighter = (NounFighter *)nouns[i].pNoun;
				if ( pFighter->weapon() == this )
				{
					pFighter->setTarget( NULL );
					bFighterRecalled = true;
				}
			}
		}

		if( bFighterRecalled )
			message( "<color;ffffff>Command: Fighters returning to base." );
	}
	else
	{
		// launch a fighter
		GadgetWeapon::use( when, pTarget, shift );
	}
}

NounProjectile * GadgetFighterBay::projectileFactory()
{
	// get the fighter template
	Scene * pScene = WidgetCast<Scene>( nounContext()->resource( "FIGHTER" ) );
	if ( pScene == NULL )
		return NULL;
	if ( pScene->node()->childCount() < 1 )
		return NULL;

	// find the NounFighter object in the scene 
	NounFighter * pFighter = WidgetCast<NounFighter>( pScene->node()->child( 0 ) );
	if ( pFighter != NULL )
	{
		// return a copy of the fighter object!
		NounFighter * pCopy = (NounFighter *)pFighter->copy();
		// load all ammo
		pCopy->setup();

		return pCopy;
	}

	return NULL;		// no fighter found in scene
}

//----------------------------------------------------------------------------
//EOF
